import { delegate } from "../../Framework/scripts/DefineData";


const { ccclass, property } = cc._decorator;

@ccclass
export default class CanDrag extends cc.Component {


    private _on_draged_event: delegate = null;
    private _on_drag_event:delegate = null;

    add_draged_event(func: Function, caller: any) {

        if (this._on_draged_event == null)
            this._on_draged_event = new delegate(func, caller);
        else
            this._on_draged_event.add(func, caller);
    }

    add_drag_event(func: Function, caller: any) {
        if (this._on_drag_event == null)
            this._on_drag_event = new delegate(func, caller);
        else
            this._on_drag_event.add(func, caller);
    }

    onEnable() {
        this.node.on(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this.node.on(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
    }

    onDisable() {
        this.node.off(cc.Node.EventType.TOUCH_MOVE, this._onTouchMove, this);
        this.node.off(cc.Node.EventType.TOUCH_END, this._onTouchEnd, this);
    }

    _onTouchMove(touchEvent) {
        let location = touchEvent.getLocation();
        this.node.position = this.node.parent.convertToNodeSpaceAR(location); // 确定位置
        this._on_drag_event && this._on_drag_event.invoke();
    }

    _onTouchEnd(touchEvent) {

        this._on_draged_event && this._on_draged_event.invoke();
    }
}
